#include <bits/stdc++.h>
using namespace std;

struct SegmentTree {
  static const int N = (1 << 18) + 10;
  int n, nn;      // n:输入的幂次, nn:实际元素数量(2^n)
  int f[N << 1];  // 线段树数组，存储每个节点的最小值
  int l[N], r[N]; // 左右子节点索引数组

  void dfs(int x) {
    if (x < nn)
      dfs(l[x]), dfs(r[x]);
    else
      cout << f[x] << " ";
  }

  void build(int n) {

    nn = (1 << n);

    // 读取输入数据并存储在叶子节点
    for (int i = 0; i < nn; i++)
      cin >> f[nn + i];

    // 自底向上构建线段树
    for (int i = nn - 1; ~i; i--) {
      l[i] = (i << 1);
      r[i] = (i << 1 | 1);
      if (f[l[i]] > f[r[i]])
        swap(l[i], r[i]);
      f[i] = min(f[l[i]], f[r[i]]);
    }
  }

  void print() {
    dfs(1);
    cout << "\n";
  }
};

signed main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    SegmentTree st;
    st.build(n);
    st.print();
  }
  return 0;
}